package com.house.tgroup.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.house.tgroup.entity.GroupEntity;
import com.house.tgroup.service.GroupService;
import com.house.tgroup.vo.UnitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.house.tgroup.entity.GroupDescEntity;
import com.house.tgroup.service.GroupDescService;
import com.house.common.utils.PageUtils;
import com.house.common.utils.R;



/**
 * 
 *
 * @author wzh
 * @email sunlightcs@gmail.com
 * @date 2022-04-27 17:35:02
 */
@RestController
@RequestMapping("tgroup/groupdesc")
public class GroupDescController {
    @Autowired
    private GroupDescService groupDescService;
    @Autowired
    private GroupService groupService;


    @RequestMapping("/info/{id}")
    public R list(@PathVariable("id")Integer id){
        QueryWrapper<GroupDescEntity> w = new QueryWrapper<>();
        w.eq("group_id",id);
        List<GroupDescEntity> list = groupDescService.list(w);
        return R.ok().put("list",list);
    }

    //查询所有的详情表，看当前登录人是否已经添加过该团购，如果添加不允许再添加此团购
    @RequestMapping("/findAllByIdAndName")
    public R findAllByIdAndName(@RequestParam("id")Integer id,@RequestParam("name")String name){
        return groupDescService.findAllByIdAndName(id,name);
    }

    //添加团购  判断 人数是否已满
    //修改团购数量
    //添加详情表记录
    //传id 和当前登录人名字
    //后台做添加
    @RequestMapping("/addUser")
    public R addUser(@RequestParam("id")Integer id, @RequestParam("uname")String uname, @RequestParam("submoney")BigDecimal submoney){
        return groupDescService.addUser(id,uname,submoney);
    }

    //退团

    @RequestMapping("/outT/{id}")
    public R outT(@PathVariable("id")Integer id){
        GroupDescEntity groupdesc = groupDescService.getById(id);
        groupdesc.setStatus(1);
        groupDescService.updateById(groupdesc);
        return R.ok();
    }
    @RequestMapping("/delete/{id}")
    public R delete(@PathVariable("id")Integer id){
        GroupDescEntity groupdesc = groupDescService.getById(id);
        GroupEntity group = groupService.getById(groupdesc.getGroupId());
        group.setPeoCont(group.getPeoCont()-1);
        groupService.updateById(group);
        groupDescService.removeById(id);
        return R.ok();
    }



}
